package org.sunsys.pdfbox.page.reader;

import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;

public class PdfPagesReader {

	private PDDocument document = null;
	private PDFTextStripper textStripper = null;
	
	public PdfPagesReader(String pdfName) throws IOException {
		/* load document */
		document = PDDocument.load(pdfName);
		
		/* create instance of Pdf Text Stripper - which can be use to read text from Pdf */
		textStripper = new PDFTextStripper();
	}
	
	public int getNumberOfPages() {
		return this.document.getNumberOfPages();
	}
	
	/**
	 * returns tex, pageNo 's from pdf
	 * @param pageNo int
	 * @return String
	 * @throws IOException
	 */
	public String getPageText(int pageNo ) throws IOException {
		
		/* set page no, from which Text has to be extracted/retrieved */
		textStripper.setStartPage(pageNo);
		textStripper.setEndPage(pageNo);
		
		/* extract text from pdf */
		return textStripper.getText(document);
	}

	/**
	 * closes the pdf document
	 */
	public void closeDocument() {
		try {
			if(this.document!=null) {
				this.document.close();
			}
		}catch(IOException e1){
		}
	}
	
	public static void main(String []args) throws IOException {
	
		String pdfName = "D:/Tutorial/WebSearch/Solr Docs/apache_solr_4_cookbook.pdf";

		PdfPagesReader pageReader = new PdfPagesReader(pdfName);
		int pageNo = 10;
		
		pageReader.getPageText(pageNo);

		System.out.println("Thank you");
		
	}
}
